//
//  UserInfoModel.m
//  ChatForHot
//
//  Created by FLY on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "UserInfoModel.h"

@implementation UserInfoModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
    if ([key isEqualToString:@"accountDto"]) {
        [self setValuesForKeysWithDictionary:value];
    }else{
        //        [super setValue:value forUndefinedKey:key];
    }
}

- (void)setValue:(id)value forKey:(NSString *)key {
    if ([value isKindOfClass:[NSNumber class]]) {
        [super setValue:(((NSNumber *)value).stringValue) forKey:key];
    }else{
        [super setValue:value forKey:key];
    }
}

- (void)HOTCHATsaveTheUserInfo{
    
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setValue:[NSString stringWithFormat:@"%@",self.IMPassword] forKey:@"IMPassword"];
    [users setValue:[NSString stringWithFormat:@"%@",self.IMUser] forKey:@"IMUser"];
    [users setValue:[NSString stringWithFormat:@"%@",self.accountNumber] forKey:@"accountNumber"];
    [users setValue:[NSString stringWithFormat:@"%@",self.password] forKey:@"password"];
    [users setValue:[NSString stringWithFormat:@"%@",self.signature] forKey:@"signature"];
    [users setValue:[NSString stringWithFormat:@"%@",self.age] forKey:@"age"];
    [users setValue:[NSString stringWithFormat:@"%@",self.height] forKey:@"height"];
    [users setValue:[NSString stringWithFormat:@"%@",self.minFeet] forKey:@"minFeet"];
    [users setValue:[NSString stringWithFormat:@"%@",self.maxFeet] forKey:@"maxFeet"];
    [users setValue:[NSString stringWithFormat:@"%@",self.weight] forKey:@"weight"];
    [users setValue:[NSString stringWithFormat:@"%@",self.constellation] forKey:@"constellation"];
    [users setValue:[NSString stringWithFormat:@"%@",self.income] forKey:@"income"];
    [users setValue:[NSString stringWithFormat:@"%@",self.occupation] forKey:@"occupation"];
    
    [users setValue:[NSString stringWithFormat:@"%@",self.headPic] forKey:@"headPic"];
    [users setValue:[NSString stringWithFormat:@"%@",self.sex] forKey:@"sex"];
    
    if (self.token.length > 0) {
        [users setValue:[NSString stringWithFormat:@"%@",self.token] forKey:@"token"];
    }else{
        NSLog(@"F_kongtoken");
    }
    
    [users setValue:[NSString stringWithFormat:@"%@",self.accountid] forKey:@"accountid"];
    [users setValue:[NSString stringWithFormat:@"%@",self.nickName] forKey:@"nickName"];
    
    [users setValue:[NSString stringWithFormat:@"%@",self.commonPicCount] forKey:@"commonPicCount"];
    [users setValue:[NSString stringWithFormat:@"%@",self.privatePicCount] forKey:@"privatePicCount"];
    
    [users setValue:[NSString stringWithFormat:@"%@",self.code] forKey:@"code"];
    [users setValue:[NSString stringWithFormat:@"%@",self.isWrite] forKey:@"isWrite"];
    [users setValue:[NSString stringWithFormat:@"%@",self.isDiamonds] forKey:@"isDiamonds"];
    [users setValue:[NSString stringWithFormat:@"%@",self.isExtreme] forKey:@"isExtreme"];
    [users setValue:[NSString stringWithFormat:@"%@",self.isApprove] forKey:@"isApprove"];
    
    [users setValue:[NSString stringWithFormat:@"%@",self.addToType] forKey:@"addToType"];
    [users setValue:[NSString stringWithFormat:@"%@",self.addHiToType] forKey:@"addHiToType"];
    [users setValue:[NSString stringWithFormat:@"%@",self.approvePic] forKey:@"approvePic"];
    [users setValue:[NSString stringWithFormat:@"%@",self.commonVideoCount] forKey:@"commonVideoCount"];
    [users setValue:[NSString stringWithFormat:@"%@",self.privateVideoCount] forKey:@"privateVideoCount"];
    
    
    [[APPINFO defaultManager] HOTCHATreset];
}
+ (void)HOTCHATsaveUserInfoFromRegisterWithDic:(NSDictionary *)dic Sex:(NSInteger)sex{
    
    NSDictionary *dataDic = [dic valueForKey:@"data"];
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    //save data
    [users setValue:[NSString stringWithFormat:@"%@",dataDic[@"accountNumber"]] forKey:@"accountNumber"];
    [users setValue:[NSString stringWithFormat:@"%@",dataDic[@"accountPassword"]] forKey:@"accountPassword"];
    [users setValue:[NSString stringWithFormat:@"%@",dataDic[@"password"]] forKey:@"password"];
    [users setValue:[NSString stringWithFormat:@"%@",dataDic[@"IMUser"]] forKey:@"IMUser"];
    [users setValue:[NSString stringWithFormat:@"%@",dataDic[@"IMPassword"]] forKey:@"IMPassword"];
    
    [users setValue:@"" forKey:@"headPic"];
    [users setValue:[NSString stringWithFormat:@"%ld",(long)sex] forKey:@"sex"];
    [users setValue:@"" forKey:@"accountid"];
    
    [[APPINFO defaultManager] HOTCHATreset];
}

+ (void)HOTCHATlogout {
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setValue:@"" forKey:@"IMPassword"];
    [users setValue:@"" forKey:@"IMUser"];
    //需要记住上次登录的账号
    [users setValue:@"" forKey:@"accountNumber"];
    [users setValue:@"" forKey:@"password"];
    [users setValue:@"" forKey:@"signature"];
    [users setValue:@"" forKey:@"age"];
    [users setValue:@"" forKey:@"height"];
    [users setValue:@"" forKey:@"minFeet"];
    [users setValue:@"" forKey:@"maxFeet"];
    [users setValue:@"" forKey:@"weight"];
    [users setValue:@"" forKey:@"constellation"];
    [users setValue:@"" forKey:@"income"];
    [users setValue:@"" forKey:@"occupation"];
    
    [users setValue:@"" forKey:@"headPic"];
    [users setValue:@"" forKey:@"sex"];
    
    [users setValue:@"" forKey:@"token"];
    [users setValue:@"" forKey:@"accountid"];
    [users setValue:@"" forKey:@"nickName"];
    
    [users setValue:@"" forKey:@"commonPicCount"];
    [users setValue:@"" forKey:@"privatePicCount"];
    
    [users setValue:@"" forKey:@"code"];
    [users setValue:@"" forKey:@"isWrite"];
    [users setValue:@"" forKey:@"isDiamonds"];
    [users setValue:@"" forKey:@"isExtreme"];
    [users setValue:@"" forKey:@"isApprove"];
    
    [users setValue:@"" forKey:@"addToType"];
    [users setValue:@"" forKey:@"addHiToType"];
    [users setValue:@"" forKey:@"approvePic"];
    [users setValue:@"" forKey:@"commonVideoCount"];
    [users setValue:@"" forKey:@"privateVideoCount"];
    [[APPINFO defaultManager] HOTCHATreset];
}

//type list
+ (NSMutableArray *)HOTCHATtypeList{
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    //Normal
    [arr addObject:[[TypeModel alloc] initWithID:@"1" name:@"Normal"]];
    //Slender
    [arr addObject:[[TypeModel alloc] initWithID:@"2" name:@"Slender"]];
    //Thin
    [arr addObject:[[TypeModel alloc] initWithID:@"3" name:@"Thin"]];
    //Athletic
    [arr addObject:[[TypeModel alloc] initWithID:@"4" name:@"Athletic"]];
    //Heavyset
    [arr addObject:[[TypeModel alloc] initWithID:@"5" name:@"Heavyset"]];
    //Stocky
    [arr addObject:[[TypeModel alloc] initWithID:@"6" name:@"Stocky"]];
    
    return arr;
}
//get type info

+ (NSString *)HOTCHATgetTypeNameWith:(NSString *)typeId{
    NSMutableArray *array = [UserInfoModel HOTCHATtypeList];
    if (typeId.integerValue <= array.count) {
        TypeModel *model = [array objectOrNilAtIndex:typeId.integerValue-1];
        return model.name;
    }
    return @"";
}

/**星座list**/
+ (NSMutableArray *)HOTCHATconstellationArray{
    static NSMutableArray *HOTCHATconstellationArray = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        HOTCHATconstellationArray = [[NSMutableArray alloc] init];
    });
    if (HOTCHATconstellationArray.count == 0) {
        //Aquarius
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"1" name:NSLocalizedString(@"constellation1", nil)]];
        //Pisces
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"2" name:NSLocalizedString(@"constellation2", nil)]];
        //Aries
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"3" name:NSLocalizedString(@"constellation3", nil)]];
        //Taurus
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"4" name:NSLocalizedString(@"constellation4", nil)]];
        //Gemini
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"5" name:NSLocalizedString(@"constellation5", nil)]];
        //Cancer
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"6" name:NSLocalizedString(@"constellation6", nil)]];
        //Leo
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"7" name:NSLocalizedString(@"constellation7", nil)]];
        //Virgo
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"8" name:NSLocalizedString(@"constellation8", nil)]];
        //Libra
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"9" name:NSLocalizedString(@"constellation9", nil)]];
        //Scorpio
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"10" name:NSLocalizedString(@"constellation10", nil)]];
        //Sagittarius
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"11" name:NSLocalizedString(@"constellation11", nil)]];
        //Capricorn
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"12" name:NSLocalizedString(@"constellation12", nil)]];
        //Secrecy
        [HOTCHATconstellationArray addObject:[[TypeModel alloc] initWithID:@"13" name:NSLocalizedString(@"constellation13", nil)]];
        
        
    }
    return HOTCHATconstellationArray;
}

/**获取星座、名称**/
+ (NSString *)HOTCHATgetConstellationNameWithID:(NSString *)typeID{
    NSArray *array = [UserInfoModel HOTCHATconstellationArray];
    NSArray *filteredArray = [UserInfoModel selectFromArray:array typeID:typeID];
    
    return [UserInfoModel getNameFromArray:filteredArray defaultArray:array];
}

/**Job list**/
+ (NSMutableArray *)HOTCHATjobArray{
    static NSMutableArray *HOTCHATjobArray = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        HOTCHATjobArray = [[NSMutableArray alloc] init];
    });
    if (HOTCHATjobArray.count == 0) {
        //
        TypeModel *model1 = [[TypeModel alloc] initWithID:@"1" name:NSLocalizedString(@"job1", nil)];
        [HOTCHATjobArray addObject:model1];
        //
        TypeModel *model2 = [[TypeModel alloc] initWithID:@"2" name:NSLocalizedString(@"job2", nil)];
        [HOTCHATjobArray addObject:model2];
        //
        TypeModel *model3 = [[TypeModel alloc] initWithID:@"3" name:NSLocalizedString(@"job3", nil)];
        [HOTCHATjobArray addObject:model3];
        //
        TypeModel *model4 = [[TypeModel alloc] initWithID:@"4" name:NSLocalizedString(@"job4", nil)];
        [HOTCHATjobArray addObject:model4];
        //
        TypeModel *model5 = [[TypeModel alloc] initWithID:@"5" name:NSLocalizedString(@"job5", nil)];
        [HOTCHATjobArray addObject:model5];
        //
        TypeModel *model6 = [[TypeModel alloc] initWithID:@"6" name:NSLocalizedString(@"job6", nil)];
        [HOTCHATjobArray addObject:model6];
        //
        TypeModel *model7 = [[TypeModel alloc] initWithID:@"7" name:NSLocalizedString(@"job7", nil)];
        [HOTCHATjobArray addObject:model7];
        //
        TypeModel *model8 = [[TypeModel alloc] initWithID:@"8" name:NSLocalizedString(@"job7", nil)];
        [HOTCHATjobArray addObject:model8];
        //Law enforcement
        TypeModel *model9 = [[TypeModel alloc] initWithID:@"9" name:NSLocalizedString(@"job9", nil)];
        [HOTCHATjobArray addObject:model9];
        //Legal
        TypeModel *model10 = [[TypeModel alloc] initWithID:@"10" name:NSLocalizedString(@"job10", nil)];
        [HOTCHATjobArray addObject:model10];
        ///Student
        TypeModel *model11 = [[TypeModel alloc] initWithID:@"11" name:NSLocalizedString(@"job11", nil)];
        [HOTCHATjobArray addObject:model11];
        //NGO
        TypeModel *model12 = [[TypeModel alloc] initWithID:@"12" name:NSLocalizedString(@"job12", nil)];
        [HOTCHATjobArray addObject:model12];
    }
    return HOTCHATjobArray;
}

/**获取Job、名称**/
+ (NSString *)HOTCHATgetJobNameWithID:(NSString *)typeID{
    NSArray *array = [UserInfoModel HOTCHATjobArray];
    NSArray *filteredArray = [UserInfoModel selectFromArray:array typeID:typeID];
    
    return [UserInfoModel getNameFromArray:filteredArray defaultArray:array];
}

/**星座list**/
+ (NSMutableArray *)HOTCHATincomeArray{
    static NSMutableArray *HOTCHATincomeArray = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        HOTCHATincomeArray = [[NSMutableArray alloc] init];
    });
    if (HOTCHATincomeArray.count == 0) {
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"1" name:NSLocalizedString(@"income1", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"2" name:NSLocalizedString(@"income2", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"3" name:NSLocalizedString(@"income3", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"4" name:NSLocalizedString(@"income4", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"5" name:NSLocalizedString(@"income5", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"6" name:NSLocalizedString(@"income6", nil)]];
        //
        [HOTCHATincomeArray addObject:[[TypeModel alloc] initWithID:@"7" name:NSLocalizedString(@"income7", nil)]];
    }
    return HOTCHATincomeArray;
}

/**income array**/
+ (NSString *)HOTCHATgetIncomeNameWithID:(NSString *)typeID{
    NSArray *array = [UserInfoModel HOTCHATincomeArray];
    NSArray *filteredArray = [UserInfoModel selectFromArray:array typeID:typeID];
    
    return [UserInfoModel getNameFromArray:filteredArray defaultArray:array];
}

+ (NSString *)getNameFromArray:(NSArray *)array defaultArray:(NSArray *)defaultArray{
    if (array.count > 0) {
        TypeModel *model = [array firstObject];
        return model.name;
        
    }
    return @"";
}

+ (NSArray *)selectFromArray:(NSArray *)array typeID:(NSString *)typeID{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"typeID == %@", typeID];
    return [array filteredArrayUsingPredicate:predicate];
}
- (NSMutableArray *)privteArr{
    
    if (_privteArr == nil) {
        NSMutableArray *priArr = [NSMutableArray array];
        if (_privateVideo.count > 0) {
            VPModel *model = [[VPModel alloc] init];
            model.isVideo = YES;
            for (NSDictionary *dic in self.privateVideo) {
                
                model.url = [dic valueForKey:@"videoAddress"];
            }
            [priArr addObject:model];
        }
        if (_privatePic.count > 0) {
            
            for (NSString *url in self.privatePic) {
                VPModel *model = [[VPModel alloc] init];
                model.isVideo = NO;
                model.url = url;
                [priArr addObject:model];
            }
            
        }
        
        _privteArr = priArr;
    }
    return _privteArr;
}
- (NSMutableArray *)commonArr{
    
    if (_commonArr == nil) {
        NSMutableArray *comArr = [NSMutableArray array];
        if (_commonVideo.count > 0) {
            
            for (NSDictionary *dic in self.commonVideo) {
                VPModel *model = [[VPModel alloc] init];
                model.isVideo = YES;
                model.url = [dic valueForKey:@"videoAddress"];
                [comArr addObject:model];
            }
            
        }
        if (_commonPic.count > 0) {
            
            for (NSString *url in self.commonPic) {
                VPModel *model = [[VPModel alloc] init];
                model.isVideo = NO;
                model.url = url;
                [comArr addObject:model];
            }
            
        }
        if (comArr.count == 0) {
            //no common video and pics
            VPModel *model = [[VPModel alloc] init];
            model.isVideo = NO;
            
            model.url = self.headPic;
            [comArr addObject:model];
        }
        _commonArr = comArr;
    }
    
    return _commonArr;
}
- (void)HOTCHATsaveIMUserInfo{
    DBUtility *dbutility = [DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDeferredTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        NSString *sql = @"delete from IMInfo where accountid = :accountid";
        NSMutableDictionary *infoDic = [[NSMutableDictionary alloc] init];
        [infoDic setValue:self.accountid forKey:@"accountid"];
        if ( ![db executeUpdate:sql withParameterDictionary:infoDic] ) {
            //            DB_ERROR_LOG(NSStringFromClass([self class]))
            *rollback = YES;
            return;
        }
        
        sql = @"insert into IMInfo(accountid,IMUser,nickName,headPic,vip,signal) values(:accountid,:IMUser,:nickName,:headPic,:vip,:signal)";
        
        
        [infoDic setValue:self.IMUser forKey:@"IMUser"];
        [infoDic setValue:self.nickName forKey:@"nickName"];
        [infoDic setValue:self.headPic forKey:@"headPic"];
        [infoDic setValue:self.code forKey:@"vip"];
        [infoDic setValue:self.signature forKey:@"signal"];
        
        if ( ![db executeUpdate:sql withParameterDictionary:infoDic] ) {
            //            DB_ERROR_LOG(NSStringFromClass([self class]))
            //            *rollback = YES;
            return;
        }
        
    }];
    [dbutility close];
}

- (BOOL)HOTCHATgetIMImfoFromDBByImUser:(NSString *)imUser{
    __block BOOL isHave = NO;
    DBUtility *dbutility = (DBUtility *)[DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDatabase:^(FMDatabase *db) {
        
        @try {
            
            NSString *sql =[NSString stringWithFormat:@" SELECT * from IMInfo where IMUser = :IMUser "];
            
            NSDictionary *infoDic = [[NSDictionary alloc] initWithObjectsAndKeys:imUser,@"IMUser", nil];
            
            FMResultSet *results = [db executeQuery:sql withParameterDictionary:infoDic];
            
            while( [results next] ){
                isHave = YES;
                self.accountid = [results stringForColumn:@"accountid"];
                self.IMUser = [results stringForColumn:@"IMUser"];
                self.code = [results stringForColumn:@"vip"];
                self.headPic = [results stringForColumn:@"headPic"];
                self.nickName = [results stringForColumn:@"nickName"];
                self.signature = [results stringForColumn:@"signal"];
                [results close];
            }
        }
        @catch (NSException *exception) {
            NSLog(@"%@",NSStringFromClass([self class]));
        }
        
    }];
    [dbutility close];
    return isHave;
}

@end

@implementation TypeModel

- (instancetype)initWithID:(NSString *)typeID name:(NSString *)name{
    if (self = [super init]) {
        self.typeID = typeID;
        self.name = name;
    }
    return self;
}
@end

@implementation VPModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
    
}

@end
